package com.luke.star.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.luke.star.bean.BaseResBean;
import com.luke.star.bean.Pager;
import com.luke.star.bean.TaskProcessBean;
import com.luke.star.persist.po.StAssignment;
import com.luke.star.persist.po.StTask;
import com.luke.star.serviceImpl.StTaskService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
public class StTaskController {

	@Resource
	private StTaskService stTaskService;
	
	@RequestMapping(value="/queryTaskByPage",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String queryTaskByPage(Pager pager,String taskName){
		log.info("开始处理任务查询请求");
		
		Map<String,Object> queryParam = new HashMap<>();
		queryParam.put("begin", pager.getBegin());
		queryParam.put("pageSize", pager.getPageSize());
		if(!StringUtils.isEmpty(taskName)) {
			queryParam.put("taskName", taskName);
		}
		
		Pager page = stTaskService.queryTaskByPage(pager, queryParam);

		return JSON.toJSONStringWithDateFormat(page,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat);
	}
	
	@RequestMapping(value="/queryTaskLogByPage",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String queryTaskLogByPage(Pager pager,String taskName,String createTimeBegin,String createTimeEnd){
		log.info("开始处理任务日志查询请求");
		
		Map<String,Object> queryParam = new HashMap<>();
		queryParam.put("begin", pager.getBegin());
		queryParam.put("pageSize", pager.getPageSize());
		if(!StringUtils.isEmpty(taskName)) {
			queryParam.put("taskName", taskName);
		}
		if(!StringUtils.isEmpty(createTimeBegin)) {
			queryParam.put("createTimeBegin", createTimeBegin);
		}
		if(!StringUtils.isEmpty(createTimeEnd)) {
			queryParam.put("createTimeEnd", createTimeEnd);
		}
		
		Pager page = stTaskService.queryTaskLogByPage(pager, queryParam);
		
		return JSON.toJSONStringWithDateFormat(page,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat);
	}
	
	@RequestMapping(value="/addTask",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addTask(StTask record){
		log.info("开始处理任务添加请求");
		
		BaseResBean result = stTaskService.addTaskByHuman(record);

		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value="/updateProcess",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateProcess(TaskProcessBean reqBean){
		log.info("开始处理任务添加请求");
		
		BaseResBean result = stTaskService.updateProcess(reqBean);

		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value="/addAssignment",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addAssignment(StAssignment record){
		log.info("开始处理任务分解添加请求");
		
		BaseResBean result = stTaskService.addAssignmentByHuman(record);

		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value="/updateAssignment",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateAssignment(StAssignment reqBean){
		log.info("开始处理任务分解修改请求");
		
		BaseResBean result = stTaskService.updateAssignmentByHuman(reqBean);

		return JSON.toJSONString(result);
	}
	
}
